package com.lan.lineage.tools;

import com.lan.lineage.common.LineageDetail;
import java.util.List;
import java.util.stream.Collectors;

public class Metrics {

    public static double getColumnNumberSimiliarity(LineageDetail l1, LineageDetail l2) {
        return 1.0 / (Math.abs(l1.getSelectColumnNumber() - l2.getSelectColumnNumber()) + 1);
    }

    public static double getDbNameSimiliarity(LineageDetail l1, LineageDetail l2) {
        List<String> list1 = l1.getDbNameList();
        List<String> list2 = l2.getDbNameList();
        List<String> intersection = list1.stream().filter(list2::contains).collect(Collectors.toList());
//        System.out.println(Arrays.toString(list1.toArray()));
//        System.out.println(Arrays.toString(list2.toArray()));
//        System.out.println(Arrays.toString(intersection.toArray()));
        if (list1.size() == 0 && list2.size() == 0) {
            return 1.0;
        } else {
            return (double) intersection.size() / Math.max(list1.size(), list2.size());
        }
    }

    public static double getTableNameSimiliarity(LineageDetail l1, LineageDetail l2) {
        List<String> list1 = l1.getTableNameList();
        List<String> list2 = l2.getTableNameList();
        List<String> intersection = list1.stream().filter(list2::contains).collect(Collectors.toList());
        if (intersection.size() > 0 && intersection.get(0).equals("")) {
            return (double) (intersection.size() - 1) / Math.max(list1.size(), list2.size());
        }
        return (double) intersection.size() / Math.max(list1.size(), list2.size());
    }

    public static double getColumnNameSimiliarity(LineageDetail l1, LineageDetail l2) {
        List<String> list1 = l1.getColumnNameList();
        List<String> list2 = l2.getColumnNameList();
        List<String> intersection = list1.stream().filter(list2::contains).collect(Collectors.toList());
        return (double) intersection.size() / Math.max(list1.size(), list2.size());
    }

    public static double getSimilarity(LineageDetail lineage1, LineageDetail lineage2) {
        return 25.0 * (Metrics.getColumnNumberSimiliarity(lineage1, lineage2)
                + Metrics.getDbNameSimiliarity(lineage1, lineage2)
                + Metrics.getTableNameSimiliarity(lineage1, lineage2)
                + Metrics.getColumnNameSimiliarity(lineage1, lineage2));
    }
}
